package com.ruoyi.project.ypjxc.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ypjxc.domain.Drug;
import com.ruoyi.project.ypjxc.domain.DrugType;
import com.ruoyi.project.ypjxc.domain.YFactory;
import com.ruoyi.project.ypjxc.service.YpjxcDrugService;

import com.ruoyi.project.ypjxc.service.YpjxcFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ypjxc/drug")
public class YpjxcDrugController extends BaseController {
    @Autowired
    private YpjxcDrugService drugService;
    @Autowired
    private YpjxcFactoryService factoryService;
    /**
     * 获取药品列表
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drug drug) {
        startPage();
        List<Drug> list = drugService.selectDrugList(drug);
        return getDataTable(list);
    }

    /**
     * 查询所有工厂信息
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:query')")
    @GetMapping("/getfactory")
    public AjaxResult getfactory()
    {
        List<YFactory> yFactories = factoryService.selectYFactoryList(new YFactory());
        return AjaxResult.success(yFactories);
    }

    /**
     * 查询所有药品类型信息
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:query')")
    @GetMapping("/getDrugType")
    public AjaxResult getDrugType()
    {
        List<DrugType> drugTypes = drugService.selectDrugTypeList(new DrugType());
        return AjaxResult.success(drugTypes);
    }

    /**
     * 根据id查询
     * @param
     * @return
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:query')")
    @GetMapping
    public AjaxResult getfactoryName()
    {
        if (drugService.selectYFactoryNameList()==null){
            return AjaxResult.error("没有查询到工厂名称,请添加工厂信息后再查询");
        }
        return AjaxResult.success(drugService.selectYFactoryNameList());
    }

    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:add')")
    @Log(title = "药品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Drug drug) {
        if (drug==null){
            return AjaxResult.error("信息不能为空");
        }
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugService.insertDrug(drug));
    }

    /**
     * 修改药品
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:edit')")
    @Log(title = "药品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Drug drug)
    {
        if (drug==null){
            return AjaxResult.error("信息不能为空");
        }
        drug.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugService.updateDrug(drug));
    }

    /**
     * 根据id查询
     * @param drugId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:query')")
    @GetMapping(value = "/{drugId}")
    public AjaxResult getInfo(@PathVariable Long drugId)
    {
        return AjaxResult.success(drugService.selectDrugById(drugId));
    }

    /**
     * 删除药品信息
     * @param drugIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:drug:remove')")
    @Log(title = "药品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugIds}")
    public AjaxResult delete(@PathVariable Long[] drugIds){
        return toAjax(drugService.deleteDrugByIds(drugIds));
    }


}
